<?php

declare(strict_types=1);
/**
 * This file is part of habo_new_media.
 *
 * @link     https://www.berlen.cn/
 * @contact  tech@berlen.cn
 * @license  http://gitlab.rehou.cn/habo/habo_new_media/-/blob/master/README.md
 */
namespace app\service\report;

use app\model\PushReportRecordModel;
use app\model\ReportRecordModel;
use app\model\ReportRecordPushModel;
use app\service\BaseService;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;

abstract class ReportBaseService extends BaseService
{
    /**
     * @var null|ReportRecordPushModel 发送模型
     */
    protected ?ReportRecordPushModel $pullModel;

    /**
     * @var null|ReportRecordModel 报表模型
     */
    protected ?ReportRecordModel $reportModel;

    /**
     * 获取报表模型属性.
     *
     * @param null|mixed $field
     * @return null|mixed|ReportRecordModel
     */
    public function getReportAttr($field = null)
    {
        if (is_null($field)) {
            return $this->reportModel;
        }
        return $this->reportModel->getAttr($field);
    }

    /**
     *  获取拉取数据模型属性.
     * @param null|mixed $field
     * @return null|mixed|ReportRecordPushModel
     */
    public function getPullAttr($field = null)
    {
        if (is_null($field)) {
            return $this->pullModel;
        }
        return $this->pullModel->getAttr($field);
    }

    /**
     * 获取报表发送模型.
     *
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    protected function setReportModel(): bool
    {
        $this->reportModel = (new ReportRecordModel())->find($this->pullModel->getAttr('report_record_id'));
        if (! $this->reportModel instanceof ReportRecordModel || ! $this->reportModel->getKey()) {
            return self::setError('报表数据查询失败');
        }
        return true;
    }
}
